﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OLTS.DAL;
using OLTS.MODEL;

namespace OLTS.DATA_SERVICE
{
    public class WorkDetail_Service
    {
        private OTS_WORK_DETAIL _objApp = new OTS_WORK_DETAIL();
        private String _idEmp;
        public WorkDetail_Service(OTS_WORK_DETAIL objApp)
        {
            _objApp = objApp;
        }
        public WorkDetail_Service()
        {

        }
        public WorkDetail_Service(String Empid)
        {
            _idEmp = Empid;
        }

        public List<OTS_WORK_DETAIL> GetAll()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_WORK_DETAIL.ToList();
                return list;
            }
        }

        public List<OTS_WORK_DETAIL> GetByCurrentUserandDate(string emp_id, DateTime date)
        {
            
            var result = new List<OTS_WORK_DETAIL>();
            using (var ctx = new Entities())
            {
                var parent = ctx.OTS_TIME_SHEET.Include("OTS_WORK_DETAIL").Where(x => x.EMPLOYEE_ID == emp_id && x.DATE == date).FirstOrDefault();
                if (parent != null)
                {
                    var child = parent.OTS_WORK_DETAIL.ToList();
                    result = child.ToList();
                }
            }
            return result;
        }

        public void Edit()
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_WORK_DETAIL.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                obj.AMOUNT_OF_TIME = _objApp.AMOUNT_OF_TIME;
                obj.AMOUNT_OF_TIME_OVER_TIME = _objApp.AMOUNT_OF_TIME_OVER_TIME;
                obj.DETAIL = _objApp.DETAIL;
                ctx.SaveChanges();
            }
        }

        public void Add()
        {
            using (var ctx = new Entities())
            {
                ctx.OTS_WORK_DETAIL.AddObject(_objApp);
                ctx.SaveChanges();
            }
        }

        public void Delete()
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_WORK_DETAIL.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                ctx.OTS_WORK_DETAIL.DeleteObject(obj);
                ctx.SaveChanges();
            }

        }

    }
}
